﻿using Autofac;
using Autofac.Extensions.DependencyInjection;
using Dyao.Book.Core;
using Dyao.Book.Web.Extensions;
using Foundation.Core;
using Foundation.EntityFrameworkCore;
using Foundation.Service.Bus.Core;
using Foundation.Service.Bus.RabbitMQ;
using Foundation.Spider;
using Foundation.Spider.Scheduler;
using Foundation.Spider.Scheduler.Component;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Xunit.DependencyInjection.AspNetCoreTesting;
using Xunit.DependencyInjection.Logging;

[assembly: CollectionBehavior(DisableTestParallelization = true)]
namespace Dyao.Book.Tests
{
    public class Startup
    {
        public void ConfigureHost(IHostBuilder hostBuilder)
        {
            var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

            hostBuilder.ConfigureAppConfiguration(lb => lb.AddJsonFile("appsettings.json", false, true))
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureWebHost(webHostBuilder => webHostBuilder
                .UseTestServerAndAddDefaultHttpClient(o =>
                {
                    o.BaseAddress = new Uri("https://www.qidian.com/");
                })
                .UseStartup<AspNetCoreStartup>())
                .UseQueueDistinctBfsScheduler<HashSetDuplicateRemover>()
                .ConfigureContainer<ContainerBuilder>(builder =>
                {
                    builder.AddAutofacServices();
                });
        }

        private class AspNetCoreStartup
        {
            private IConfiguration configuration;
            public AspNetCoreStartup(IConfiguration configuration)
            {
                this.configuration = configuration;
            }
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddLogging(loggerBuilder =>
                {
                    loggerBuilder.AddLog4Net();
                    loggerBuilder.AddXunitOutput();
                });

                services.AddDbContext(configuration);
                services.AddSingleton<ICurrentUser, CurrentUser>(); // 用户
                services.AddObjectMapping();
                services.AddReplacement(configuration);
                services.AddEntityFrameworkCoreReplacement(configuration);

                services.AddServiceBus(configuration, busConfig =>
                {
                    busConfig.UsePublisher(services, configuration, publisher =>
                    {
                        publisher.UseRabbitMQ(services);
                    });

                });
                services.AddSpider(op =>
                {
                    op.Speed = 1;
                });
            }

            public void Configure(IApplicationBuilder app)
            {
                app.UseObjectMapping();
            }
        }
    }
}
